package com.huanggf.meituan.order;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.huanggf.meituan.R;
import com.huanggf.meituan.fooddetail.FoodDetails;
import com.huanggf.meituan.utils.DateFormatUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AllOrderActivity extends AppCompatActivity {

    private ListView all_order;
    private SQLiteDatabase db;

    private FoodDetails getFoodShopById(int food_id) {
        String selOrderShop = "SELECT id,shop_logo,shop_name,discount1,discount2,discount3,discount4 FROM food_detail " +
                " WHERE id=?";
        Cursor c = db.rawQuery(selOrderShop, new String[]{String.valueOf(food_id)});
        FoodDetails foodDetails = new FoodDetails();
        while (c.moveToNext()) {
            foodDetails.setId(c.getInt(0));
            foodDetails.setShopLogo(c.getString(1));
            foodDetails.setShopName(c.getString(2));
            foodDetails.setDiscount1(c.getString(3));
            foodDetails.setDiscount2(c.getString(4));
            foodDetails.setDiscount3(c.getString(5));
            foodDetails.setDiscount4(c.getString(6));
        }
        c.close();
        return foodDetails;
    }

    private List<Merchandise> getMerchandisesByOrderId(int order_id) {
        String selOrderShop = "SELECT m.id, m.m_name, m.m_img_path, m.price, m.food_id FROM merchandise m " +
                "INNER JOIN order_merchandise om ON m.id=om.merchandise_id WHERE om.m_order_id=?";
        Cursor c = db.rawQuery(selOrderShop, new String[]{String.valueOf(order_id)});
        List<Merchandise> merchandises = new ArrayList<>();
        while (c.moveToNext()) {
            Merchandise merchandise = new Merchandise();
            merchandise.setId(c.getInt(0));
            merchandise.setM_name(c.getString(1));
            merchandise.setM_img_path(c.getString(2));
            merchandise.setPrice(c.getDouble(3));
            merchandise.setFood_id(c.getInt(4));
            merchandises.add(merchandise);
        }
        c.close();
        return merchandises;
    }

    private List<Order> getOrderList() {
        String selOrderShop = "SELECT id,food_id,create_time,flag,reserve,reserve_time FROM m_order";
        Cursor c = db.rawQuery(selOrderShop, null);
        List<Order> orders = new ArrayList<>();
        while (c.moveToNext()) {
            Order order = new Order();
            order.setId(c.getInt(0));
            order.setFood_id(c.getInt(1));
            order.setCreate_time(c.getString(2));
            if (c.getString(3).trim().equals("1"))
                order.setFlag("已完成");
            else
                order.setFlag("未完成");
            if (c.getString(4).trim().equals("1")) {
                order.setReserve("接受预定");
                order.setReserve_time(c.getString(5));
            }
            orders.add(order);
        }
        c.close();
        return orders;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_all_layout);
        all_order = (ListView) findViewById(R.id.all_order);
        //构造ListView数据
        File file = new File("/data/data/com.huanggf.meituan/order_detail");
        if (!file.exists())
            file.mkdirs();
        //创建数据库
        db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.huanggf.meituan/databases/meituan.db", null);

        //造数据开始
      try {
            InputStream inputStream = getResources().openRawResource(R.raw.merchandise1);
            byte[] datas = new byte[inputStream.available()];
            inputStream.read(datas);
            inputStream.close();
            FileOutputStream outShopLogo = new FileOutputStream("/data/data/com.huanggf.meituan/order_detail/merchandise1.png");
            outShopLogo.write(datas);
            outShopLogo.close();
        } catch (IOException e) {
            Log.e("IOException", e.getMessage());
        }
        try {
            InputStream inputStream = getResources().openRawResource(R.raw.merchandise2);
            byte[] datas = new byte[inputStream.available()];
            inputStream.read(datas);
            inputStream.close();
            FileOutputStream outShopLogo = new FileOutputStream("/data/data/com.hanz.meituan/order_detail/merchandise2.png");
            outShopLogo.write(datas);
            outShopLogo.close();
        } catch (IOException e) {
            Log.e("IOException", e.getMessage());
        }
        try {
            InputStream inputStream = getResources().openRawResource(R.raw.merchandise3);
            byte[] datas = new byte[inputStream.available()];
            inputStream.read(datas);
            inputStream.close();
            FileOutputStream outShopLogo = new FileOutputStream("/data/data/com.hanz.meituan/order_detail/merchandise3.png");
            outShopLogo.write(datas);
            outShopLogo.close();
        } catch (IOException e) {
            Log.e("IOException", e.getMessage());
        }

        //商品数据
        ContentValues test = new ContentValues();
        test.put("m_name","咸蛋黄蛋糕");
        test.put("m_img_path", "/data/data/com.hanz.meituan/order_detail/merchandise1.png");
        test.put("price",33.0);
        test.put("food_id",1);
        db.insert("merchandise", null, test);

        ContentValues test2 = new ContentValues();
        test2.put("m_name","鸭血粉丝汤");
        test2.put("m_img_path", "/data/data/com.hanz.meituan/order_detail/merchandise2.png");
        test2.put("price",12.0);
        test2.put("food_id",2);
        db.insert("merchandise", null, test2);

        ContentValues test3 = new ContentValues();
        test3.put("m_name","鸡汁汤包");
        test3.put("m_img_path", "/data/data/com.hanz.meituan/order_detail/merchandise3.png");
        test3.put("price",8.0);
        test3.put("food_id",2);
        db.insert("merchandise", null, test3);

        //订单数据
        ContentValues dd = new ContentValues();
        dd.put("food_id",1);
        dd.put("create_time", DateFormatUtil.getDate(DateFormatUtil.YMDHMS));
        dd.put("flag","1");
        dd.put("reserve","1");
        dd.put("reserve_time",DateFormatUtil.getDate(DateFormatUtil.HM));
        db.insert("m_order", null, dd);

        ContentValues om = new ContentValues();
        om.put("merchandise_id",1);
        om.put("m_order_id",1);
        db.insert("order_merchandise", null, om);

        //订单2
        ContentValues dd2 = new ContentValues();
        dd2.put("food_id",2);
        dd2.put("create_time",DateFormatUtil.getDate(DateFormatUtil.YMDHMS));
        dd2.put("flag","1");
        dd2.put("reserve","0");
        db.insert("m_order", null, dd2);

        ContentValues om1 = new ContentValues();
        om1.put("merchandise_id",2);
        om1.put("m_order_id",2);
        db.insert("order_merchandise", null, om1);

        ContentValues om2 = new ContentValues();
        om2.put("merchandise_id",3);
        om2.put("m_order_id",2);
        db.insert("order_merchandise", null, om2);
        //造数据结束
        List<Order> orders = getOrderList();
        for (Order order : orders) {
            order.setFoodDetails(getFoodShopById(order.getFood_id()));
            order.setOrderMerchandises(getMerchandisesByOrderId(order.getId()));
        }
        OrderAdapter orderAdapter = new OrderAdapter(this, orders);
        all_order.setAdapter(orderAdapter);


    }



}
